//
//  UIBarButtonItem+Extension.swift
//  weiboxiangmu
//
//  Created by wei_jingyun on 15/7/9.
//  Copyright © 2015年 wei_jingyun. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
//    // 创建普通按钮式的 UIBarButtonItem 有高亮图片
//    convenience init(imageName: String, highlightedImageName: String?){
//        let btn = UIButton()
//        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal);
//        let hImageName = highlightedImageName ?? imageName + "_highlighted"
//        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
//        btn.sizeToFit()
//        self.init(customView: btn)
//    }
    
    ///  创建 UIBarButtonItem
    ///
    ///  :param: imageName            图像名
    ///  :param: highlightedImageName 高亮名，可以为 nil
    ///  :param: target               target
    ///  :param: actionName           actionName
    convenience init(imageName: String, highlightedImageName: String?, target: AnyObject? = nil, actionName: String? = nil) {
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal);
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }
    
}

